package com.xiyu.service.repository.mailbox.label;

import com.xiyu.service.model.mailbox.label.MailboxLabel;
import com.xiyu.service.model.mailbox.label.MailboxLabelTable;
import org.babyfish.jimmer.spring.repository.JRepository;

import java.util.Optional;
import java.util.List;

public interface MailboxLabelRepository extends JRepository<MailboxLabel, Long> {
    MailboxLabelTable mailboxLabelTable = MailboxLabelTable.$;

    default void updateSort(Long id, Long sort){
        sql().createUpdate(mailboxLabelTable)
                .where(mailboxLabelTable.id().eq(id))
                .set(mailboxLabelTable.sort(), sort)
                .execute();
    }

    default List<MailboxLabel> findByOwnerId(Long ownerId){
        return sql().createQuery(mailboxLabelTable)
                .where(mailboxLabelTable.ownerId().eq(ownerId))
                .orderBy(mailboxLabelTable.sort())
                .select(mailboxLabelTable)
                .execute();
    };

    Optional<MailboxLabel> findFirstByOwnerIdAndName(Long ownerId, String name);

}